package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 八卦搜索
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class BaGuaSearchActivity extends RequestActivity {

	LayoutInflater mInflater;
	LinearLayout linlayer;
	boolean sendmessage = true;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		ImageView back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		linlayer = (LinearLayout) this.findViewById(R.id.linlayer);

		EditText editText = (EditText) this.findViewById(R.id.editText);
		editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		editText.setOnEditorActionListener(new OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (event != null
						&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
					if (sendmessage) {
						sendmessage = false;
						Intent intent = new Intent();
						intent.putExtra("key", v.getText().toString());
						setResult(3, intent);
						finish();
					} else {
						sendmessage = true;
					}
					return true;
				}
				return false;
			}
		});

		launchRequest(WooRequestFactory.getSearchBagua());
	}

	private void addView(LinearLayout layer, String[] str) {

		List<TextView> listText = new ArrayList<TextView>();

		List<View> listtextview = new ArrayList<View>();
		for (int i = 0; i < str.length; i++) {
			mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.textviewitem, null);
			final String text = str[i];
			TextView textview = (TextView) view.findViewById(R.id.textview);

			textview.setTextColor(Color.rgb(66, 66, 66));
			textview.setBackgroundResource(R.drawable.weixuanzhong);
			textview.setText(text);
			textview.setTag(text);
			textview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.putExtra("key", text);
					setResult(3, intent);
					finish();
				}
			});

			Paint paint = new Paint();
			paint.setTextSize(textview.getTextSize());
			float size = paint.measureText(textview.getText().toString());
			view.setTag((int) size + Utils.dip2px(this, 10 * 2 + 10));
			textview.setTag(i);
			listText.add(textview);
			listtextview.add(view);
		}

		int j = 0;
		LinearLayout llayout = null;
		int MaxWidth = MainActivity.widthPixels - Utils.dip2px(this, 20);
		int temporarywidth = 0;
		while (true) {
			View view = null;
			int tagwidth = 0;
			if (j == listtextview.size()) {
				return;
			} else {
				view = listtextview.get(j);

				tagwidth = (Integer) view.getTag();
			}
			if (j == 0) {
				llayout = new LinearLayout(this);
			}
			System.out.println("textview:" + tagwidth + "temporarywidth:"
					+ temporarywidth + "MaxWidth:" + MaxWidth);
			if (tagwidth + temporarywidth + 40 < MaxWidth) {
				temporarywidth += tagwidth;
				llayout.addView(view);
			} else {
				layer.addView(llayout);

				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(0, 15, 0, 0);

				llayout = new LinearLayout(this);
				llayout.setLayoutParams(layoutParams);
				temporarywidth = (Integer) view.getTag();

				llayout.addView(view);
			}

			if (j == listtextview.size() - 1) {
				layer.addView(llayout);
			}
			j++;
		}
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 1) {
				String text = bundle
						.getString(WooRequestFactory.SEARCH_SETING_DATA);
				if (!text.equals("")) {
					String[] str = { "" };
					if (text.contains(",")) {
						str = text.split(",");
					} else {
						str[0] = text;
					}

					addView(linlayer, str);
				}
			} else {
				Toast.makeText(this, "获取失败!", Toast.LENGTH_LONG).show();
			}

		}
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.baguasearch_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(1);
			finish();
		default:
			break;
		}
	}

}
